function cko_cumulative_kills()
   local l = UnitLevel('player')
   local xp = UnitXP('player')
   local xp_max = UnitXPMax('player')
   local k1 = 4*(l-1)*l
   local k2 = 4*l*(l+1)
   return k1 + (k2-k1)*xp/xp_max
end

function cko_kills_to_next_level()
   local l = UnitLevel('player')
   local k3 = 4*l*(l+1)
   return k3 - cko_cumulative_kills()
end

function cko_zero_difference(char_level)
   if char_level < 8 then
      return 5
   end
   if char_level < 10 then
      return 6
   end
   if char_level < 12 then
      return 7
   end
   if char_level < 16 then
      return 8
   end
   if char_level < 20 then
      return 9
   end
   if char_level < 30 then
      return 11
   end
   if char_level < 40 then
      return 12
   end
   if char_level < 45 then
      return 13
   end
   if char_level < 50 then
      return 14
   end
   if char_level < 55 then
      return 15
   end
   if char_level < 60 then
      return 16
   end
   return 17
end

function cko_mob_xp_pct(char_level,mob_level)
   local xp_base = 5*char_level+45
   zd = cko_zero_difference(char_level)
   if mob_level == 0 then
      return 0
   end
   if mob_level >= char_level then
      return (1+0.05*(mob_level-char_level))
   else
      if char_level - mob_level >=zd then
         return 0
      end
      return (1-(char_level-mob_level)/zd)
   end
end

function cko_xp_analysis()
  local x1 = cko_kills_to_next_level()
  cko_print("Kills to next level: "..x1)
  local cumu_kills = {}
  cumu_kills[5]=80
  cumu_kills[10]=360
  cumu_kills[20]=1520
  cumu_kills[30]=3480
  cumu_kills[40]=6240
  cumu_kills[50]=9800
  cumu_kills[60]=14160
  x1 = cko_cumulative_kills()
  for k,v in cumu_kills do
    if x1 < v then
      cko_print("Kills to level "..k..": "..v-x1)
    end
  end
end

function cko_xp_macro()
   cko_macro_create('XP',0,'/script cko_print(cko_cumulative_kills())')
   cko_macro_create('Level',0,'/script cko_xp_analysis()')
   cko_macro_create('PXP',0,"/script cko_print(format('Mob will yield %d pct of same-level XP',100*cko_mob_xp_pct(UnitLevel('player'),UnitLevel('target'))))")
end

